package com.polaris.live.widget

import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.constraintlayout.widget.ConstraintLayout
import com.blankj.utilcode.util.StringUtils
import com.polaris.live.R
import com.polaris.live.common.ext.dp
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.databinding.WidgetBarBinding

/**
 * BarView
 *
 * @author Created by lucas on 2023/10/27 14:44
 */
class BarView(context: Context, attributeSet: AttributeSet) :
    ConstraintLayout(context, attributeSet) {

    private val mBinding = WidgetBarBinding.inflate(LayoutInflater.from(context), this)

    private var mTitle: String? = null
    private var mTitleColor: Int = 0
    private var mCenterTitle: String? = null
    private var mCenterTitleColor: Int = 0
    private var mLeftResId: Int = 0
    private var mLeftVisible: Boolean? = null
    private var mRightResId: Int = 0
    private var mRightVisible: Boolean? = null
    private var mRight2ResId: Int = 0
    private var mRight2Visible: Boolean? = null

    private var mOnLeftClientListener: OnClickListener? = null
    private var mOnRightClientListener: OnClickListener? = null
    private var mOnRight2ClientListener: OnClickListener? = null

    init {
        val typedArray = getContext().obtainStyledAttributes(attributeSet, R.styleable.BarView)
        mTitle = typedArray.getString(R.styleable.BarView_title)
        mTitleColor = typedArray.getColor(R.styleable.BarView_titleColor, 0)
        mCenterTitle = typedArray.getString(R.styleable.BarView_centerTitle)
        mCenterTitleColor = typedArray.getColor(R.styleable.BarView_centerTitleColor, 0)
        mLeftResId = typedArray.getResourceId(R.styleable.BarView_leftSrc, 0)
        mLeftVisible = typedArray.getBoolean(R.styleable.BarView_leftVisible, true)
        mRightResId = typedArray.getResourceId(R.styleable.BarView_rightSrc, 0)
        mRightVisible = typedArray.getBoolean(R.styleable.BarView_rightVisible, mRightResId != 0)
        mRight2ResId = typedArray.getResourceId(R.styleable.BarView_right2Src, 0)
        mRight2Visible = typedArray.getBoolean(R.styleable.BarView_right2Visible, mRight2ResId != 0)
        typedArray.recycle()

        mTitle?.also { setTitle(it) }
        if (mTitleColor != 0) {
            mBinding.tvTitle.setTextColor(mTitleColor)
        }
        mCenterTitle?.also { setCenterTitle(it) }
        if (mCenterTitleColor != 0) {
            mBinding.tvCenter.setTextColor(mCenterTitleColor)
        }

        setLeftVisible(mLeftVisible == true)
        setRightVisible(mRightVisible == true)
        setRight2Visible(mRight2Visible == true)
        if (mLeftResId != 0) {
            mBinding.ivBack.setImageResource(mLeftResId)
        }
        if (mRightResId != 0) {
            mBinding.ivRight.setImageResource(mRightResId)
        }
        if (mRight2ResId != 0) {
            mBinding.ivRight2.setImageResource(mRight2ResId)
        }

        mBinding.ivBack.setOnSingleClickListener {
            val onLeftClientListener = mOnLeftClientListener
            if (onLeftClientListener != null) {
                onLeftClientListener.onClick(it)
            } else {
                if (context is Activity) {
                    context.finish()
                }
            }
        }
        mBinding.ivRight.setOnSingleClickListener {
            mOnRightClientListener?.onClick(it)
        }
        mBinding.ivRight2.setOnSingleClickListener {
            mOnRight2ClientListener?.onClick(it)
        }
    }

    fun setTitle(@StringRes resId: Int) {
        setTitle(StringUtils.getString(resId))
        mBinding.tvTitle.visibility = View.VISIBLE
    }

    fun setTitle(title: String) {
        mBinding.tvTitle.text = title
        mBinding.tvTitle.visibility = View.VISIBLE
    }

    fun setTitleColor(color: Int) {
        mBinding.tvTitle.setTextColor(color)
    }

    fun getTitleColor(): Int {
        return mTitleColor
    }

    fun setCenterTitle(@StringRes resId: Int) {
        setCenterTitle(StringUtils.getString(resId))
        mBinding.tvCenter.visibility = View.VISIBLE
    }

    fun setCenterTitle(title: String) {
        mBinding.tvCenter.text = title
        mBinding.tvCenter.visibility = View.VISIBLE
    }

    fun setLeftResource(@DrawableRes resId: Int) {
        mBinding.ivBack.setImageResource(resId)
    }

    fun setLeftVisible(visible: Boolean) {
        mLeftVisible = visible
        mBinding.ivBack.visibility = if (visible) View.VISIBLE else View.GONE
    }

    fun setRightResource(@DrawableRes resId: Int) {
        mBinding.ivRight.setImageResource(resId)
    }

    fun setRightVisible(visible: Boolean) {
        mRightVisible = visible
        mBinding.ivRight.visibility = if (visible) View.VISIBLE else View.GONE
    }

    fun setRight2Visible(visible: Boolean) {
        mRight2Visible = visible
        mBinding.ivRight2.visibility = if (visible) View.VISIBLE else View.GONE
    }

    fun setOnLeftClientListener(l: OnClickListener) {
        this.mOnLeftClientListener = l
    }

    fun setOnRightClientListener(l: OnClickListener) {
        this.mOnRightClientListener = l
    }

    fun setOnRight2ClientListener(l: OnClickListener) {
        this.mOnRight2ClientListener = l
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(48.dp, MeasureSpec.EXACTLY))
    }
}